package com.logic.modular.game.battle.info.roundInfo.dto;

import com.logic.modular.game.battle.panel.attribute.domain.PlayerHeroAttribute;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class PvpBattleStatisticsPlayerInfo {
    // 攻击方玩家id
    private Long playerId;
    // 战前总兵力
    private Long totalForceBefore;
    // 战后兵力
    private Long totalForceAfter;
    public Map<Long, HeroInfo> heroInfoMap;

    public static PvpBattleStatisticsPlayerInfo init(
            Long playerId,
            List<PlayerHeroAttribute> playerHeroList
    ) {
        // 检查参数
        if (playerHeroList == null || playerHeroList.isEmpty()) {
            return null;
        }

        long totalForceBefore = playerHeroList.stream()
                .mapToLong(PlayerHeroAttribute::getCurrentForce)
                .sum();
        Map<Long, HeroInfo> collect = playerHeroList.stream()
                .filter(item -> item.getCurrentForce() > 0)
                .map(HeroInfo::init)
                .collect(Collectors.toMap(HeroInfo::getPlayerHeroId, item -> item));
        return PvpBattleStatisticsPlayerInfo.builder()
                .playerId(playerId)
                .totalForceBefore(totalForceBefore)
                .heroInfoMap(collect).build();
    }


    public HeroInfo getHeroInfo(Long playerHeroId) {
        return heroInfoMap.get(playerHeroId);
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    public static class HeroInfo {
        @ApiModelProperty("队伍位置")
        private Integer rankOrder;
        @ApiModelProperty("武将id")
        private Long playerHeroId;
        @ApiModelProperty("武将基础id")
        private Long coreHeroId;

        @ApiModelProperty("战前兵力")
        private Long forceBefore;
        @ApiModelProperty("战后兵力")
        private Long forceAfter;

        @ApiModelProperty("所属阵营")
        private Long campId;

        @ApiModelProperty("击杀数")
        public Long killCount;

        @ApiModelProperty("总恢复")
        private Long recoverCount;

        public static HeroInfo init(PlayerHeroAttribute item) {
            return HeroInfo.builder()
                    .rankOrder(item.getRankOrder())
                    .playerHeroId(item.getId())
                    .coreHeroId(item.getCoreHeroId())
                    .forceBefore(item.getCurrentForce())
                    .forceAfter(item.getCurrentForce())
                    .killCount(0L)
                    .campId(null)
                    .build();
        }
    }

}
